<?php

class str_ImageDimensions {
    
    const typeLarge = 'L';
    const typeMedium = 'M';
    const typeOriginal = 'O';
    const typeSmall = 'S';
    const typeThumb = 'Th';
    const typeTiny = 'Ti';
    const typeXLarge = 'X';
    const typeX2Large = 'X2';
    const typeX3Large = 'X3';
    const typeCustom = 'Custon';
    
    const sizeInfinity = 999999;
    
    
    /**
     * Builds url for the appropriate image size
     * @param str_SmugMugImage $image
     * @param string $size [optional]
     * @param Array $sizes [optional]
     * @return string
     */
    public static function getUrl(str_SmugMugImage $image, $sizeType = self::typeDefault, Array $dimensions = array()) {
        
        $imgFile = $image->getId() . '_' . $image->getKey() . '-';
        
        switch ($sizeType) {
            case self::typeLarge:
            case self::typeMedium:
            case self::typeOriginal:
            case self::typeSmall:
            case self::typeThumb:
            case self::typeTiny:
            case self::typeXLarge:
            case self::typeX2Large:
            case self::typeX3Large:
            	$imgFile .= $sizeType . '.jpg';
                break;
            case self::typeCustom:
            	$dimensions = self::_filterSizes($dimensions);
            	$imgFile .=  (int)$dimensions[0] . 'x' . (int)$dimensions[1] . '.jpg';
                break;
        }
        
        return 'http://photos.smugmug.com/photos/' . $imgFile;
    }
    
    /**
     * Validates and corrects width and height
     * @param array $sizes
     * @return array
     */
    private static function _filterSizes(Array $sizes) {
        list($width, $height) = $sizes;
	    $height = isset($height) ? (int)$height : self::sizeInfinity;
        $width = isset($width) ? (int)$width : self::sizeInfinity;
        return array($width, $height);
    }
}
